package com.controller;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bean.Complex;
import com.bean.Project;
import com.service.ComplexService;
import com.service.ProjectService;

@Controller
@RequestMapping("complex")
public class ComplexAction {
	
	@Autowired
	private ComplexService service;
	@Autowired
	private ProjectService pservice;
	
	//列表页面
			@RequestMapping("toList.do")
			public String toList(String name,String complexnumber,String storey,Model model){
				Map<String, Object> map = new HashMap<String,Object>();
				if(complexnumber != null && "".equals(complexnumber.trim())){
					complexnumber = null;
				}
				if(storey != null && "".equals(storey.trim())){
					storey = null;
				}
				if(name != null && "".equals(name.trim())){
					name = null;
				}
				map.put("complexnumber", complexnumber);
				map.put("storey", storey);
				map.put("name", name);
				List<Complex> list = service.select(map);
				model.addAttribute("list", list);
				model.addAttribute("complexnumber",complexnumber);
				model.addAttribute("storey",storey);
				return"complex/list";
			}
			
			
			//跳转添加页面
			@RequestMapping("toAdd.do")
			public String toAdd(Model model){
				Map<String, Object> map = new HashMap<String,Object>();
				List<Project> list = pservice.select(map);
				model.addAttribute("list", list);
				return "complex/add";
			}
			
			
			//添加
			@RequestMapping("add.do")
			public String add(Complex complex) throws ParseException{
				service.insert(complex);
				return "redirect:toList.do";
			}
			
			
			//删除
			@RequestMapping("delete.do")
			public String delete(Integer id){
				service.deleteByPrimaryKey(id);
				return "redirect:toList.do";
			}
			
			
			
			//跳转修改
			@RequestMapping("toUpdate.do")
			public String toUpdate(Integer id,Model model){
				Map<String, Object> map = new HashMap<String,Object>();
				List<Project> list = pservice.select(map);
				model.addAttribute("list", list);
				Complex complex = service.selectByPrimaryKey(id);
				model.addAttribute("complex", complex);
				model.addAttribute("list", list);
				return "complex/update";
			}
			
			//修改
			@RequestMapping("update.do")
			public String update(Complex complex){
				service.updateByPrimaryKeySelective(complex);
				return "redirect:toList.do";
			}
	
	

}
